
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;
import javax.swing.border.Border;
import java.util.*;


public class ServerOutput extends JFrame{
	
	private JFrame frame = null;

	private JTextArea chatOutput = null;
	private JTextArea gameOutput = null;
	private JTextArea connOutput = null;
	private Vector<ImageApplet> maps = new Vector<ImageApplet>(6);
	
	private JPanel player1 = null;
	private JPanel player2 = null;
	private JPanel player3 = null;
	private JPanel player4 = null;
	private JPanel player5 = null;
	private JPanel player6 = null;
	
	private JLabel chatOutputLabel = null;
	private JLabel gameOutputLabel = null;
	private JLabel connOutputLabel = null;
	
	private SpringLayout layout = null;
	private Border tempBorder = null;
	private JScrollPane chatScroll = null;
	private JScrollPane gameScroll = null;
	private JScrollPane connScroll = null;
		
	private static ServerOutput so = null;
	
	public static synchronized ServerOutput getFrame(){
		if(so == null){
			so = new ServerOutput();
		}
		return so;
	}
	
	public void addToChat(String chatMsg){
		if(chatMsg == null){
			System.err.println("ServerOutput::addToChat(String) -> Null parameter.");
			throw new NullPointerException();
		}
		else{
			this.chatOutput.append(chatMsg.trim() + "\n");
		}
	}
	
	public void addToGame(String gameMsg){
		if(gameMsg == null){
			System.err.println("ServerOutput::addToChat(String) -> Null parameter.");
			throw new NullPointerException();
		}else{
			this.gameOutput.append(gameMsg + "\n");
		}
	}
	
	public void addToConn(String connMsg){
		if(connMsg == null){
			System.err.println("ServerOutput::addToChat(String) -> Null parameter.");
			throw new NullPointerException();
		}else{
			this.connOutput.append(connMsg + "\n");
		}
	}
	
	public JPanel getPlayer1(){
		return player1;
	}
	
	public JPanel getPlayer2(){
		return player2;
	}
	
	public JPanel getPlayer3(){
		return player3;
	}
	
	public JPanel getPlayer4(){
		return player4;
	}
	
	public JPanel getPlayer5(){
		return player5;
	}
	
	public JPanel getPlayer6(){
		return player6;
	}
	
    private void defaultMap()
    {
    	for (int i = 0; i < 6; i++)
    		maps.add(new ImageApplet());
    }
	
	private ServerOutput(){
		initComponents();
		setupFrame();
		setupPlayer1();
		setupPlayer2();
		setupPlayer3();
		setupPlayer4();
		setupPlayer5();
		setupPlayer6();
		
		defaultMap();
		player1.add(maps.get(0));
		player1.repaint();
		
		setupLabels();

		setupChatOutput();
		setupGameOutput();
		setupConnOutput();
		
		frame.setVisible(true);
	}
	
	private void initComponents(){
		frame = new JFrame();
		chatOutput = new JTextArea();
		gameOutput = new JTextArea();
		connOutput = new JTextArea();
		player1 = new JPanel();
		player2 = new JPanel();
		player3 = new JPanel();
		player4 = new JPanel();
		player5 = new JPanel();
		player6 = new JPanel();
		layout = new SpringLayout();
		chatOutputLabel = new JLabel();
		gameOutputLabel = new JLabel();
		connOutputLabel = new JLabel();
	}
	
	private void setupFrame(){
		frame.setSize(1024, 800);
		frame.setLayout(layout);
		frame.setTitle("Team 1 - Distributed Project <Battleship>");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
	}
	
	private void setupPlayer1(){
		tempBorder = BorderFactory.createRaisedBevelBorder();
			player1.setBorder(tempBorder);
		frame.add(player1);
			layout.putConstraint(SpringLayout.WEST, player1, 5, SpringLayout.WEST, frame);
			layout.putConstraint(SpringLayout.NORTH, player1, 5, SpringLayout.NORTH, frame);
			layout.putConstraint(SpringLayout.EAST, player1, 338, SpringLayout.WEST, frame);
			layout.putConstraint(SpringLayout.SOUTH, player1, 205, SpringLayout.NORTH, frame);
	}
	
	private void setupPlayer2(){

		tempBorder = BorderFactory.createRaisedBevelBorder();
			player2.setBorder(tempBorder);
		frame.add(player2);
			layout.putConstraint(SpringLayout.WEST, player2, 5, SpringLayout.EAST, player1);
			layout.putConstraint(SpringLayout.NORTH, player2, 5, SpringLayout.NORTH, frame);
			layout.putConstraint(SpringLayout.EAST, player2, 333, SpringLayout.WEST, player2);
			layout.putConstraint(SpringLayout.SOUTH, player2, 0, SpringLayout.SOUTH, player1);
	}
	
	private void setupPlayer3(){

		tempBorder = BorderFactory.createRaisedBevelBorder();
			player3.setBorder(tempBorder);
		frame.add(player3);
			layout.putConstraint(SpringLayout.WEST, player3, 5, SpringLayout.EAST, player2);
			layout.putConstraint(SpringLayout.NORTH,player3, 5, SpringLayout.NORTH, frame);
			layout.putConstraint(SpringLayout.EAST, player3, 333, SpringLayout.WEST, player3);
			layout.putConstraint(SpringLayout.SOUTH, player3, 0, SpringLayout.SOUTH, player1);
	}
	
	private void setupPlayer4(){
		tempBorder = BorderFactory.createRaisedBevelBorder();
			player4.setBorder(tempBorder);
		frame.add(player4);
			layout.putConstraint(SpringLayout.WEST, player4, 5, SpringLayout.WEST, frame);
			layout.putConstraint(SpringLayout.NORTH, player4, 5, SpringLayout.SOUTH, player1);
			layout.putConstraint(SpringLayout.SOUTH, player4, 200, SpringLayout.NORTH, player4);
			layout.putConstraint(SpringLayout.EAST, player4, 333, SpringLayout.WEST, player4);
	}
	
	private void setupPlayer5(){

		tempBorder = BorderFactory.createRaisedBevelBorder();
			player5.setBorder(tempBorder);
		frame.add(player5);
			layout.putConstraint(SpringLayout.WEST, player5, 5, SpringLayout.EAST, player4);
			layout.putConstraint(SpringLayout.NORTH, player5, 0, SpringLayout.NORTH, player4);
			layout.putConstraint(SpringLayout.EAST, player5, 333, SpringLayout.WEST, player5);
			layout.putConstraint(SpringLayout.SOUTH, player5, 0, SpringLayout.SOUTH, player4);
	}
	
	private void setupPlayer6(){

		tempBorder = BorderFactory.createRaisedBevelBorder();
			player6.setBorder(tempBorder);
		frame.add(player6);
			layout.putConstraint(SpringLayout.WEST, player6, 5, SpringLayout.EAST, player5);
			layout.putConstraint(SpringLayout.NORTH,player6, 0, SpringLayout.NORTH, player4);
			layout.putConstraint(SpringLayout.EAST, player6, 333, SpringLayout.WEST, player6);
			layout.putConstraint(SpringLayout.SOUTH, player6, 0, SpringLayout.SOUTH, player4);
	}
	
	private void setupLabels(){
		chatOutputLabel.setFont(new Font("Arial", Font.BOLD, 16));
		chatOutputLabel.setText("Server Chat Communications:");
		frame.add(chatOutputLabel);
			layout.putConstraint(SpringLayout.WEST, chatOutputLabel, 0, SpringLayout.WEST, player4);
			layout.putConstraint(SpringLayout.NORTH, chatOutputLabel, 5, SpringLayout.SOUTH, player4);
			layout.putConstraint(SpringLayout.SOUTH, chatOutputLabel, 18, SpringLayout.NORTH, chatOutputLabel);
			layout.putConstraint(SpringLayout.EAST, chatOutputLabel, 333, SpringLayout.WEST, chatOutputLabel);
		gameOutputLabel.setFont(new Font("Arial", Font.BOLD, 16));
		gameOutputLabel.setText("Server Game Communications:");
		frame.add(gameOutputLabel);
			layout.putConstraint(SpringLayout.WEST, gameOutputLabel, 5, SpringLayout.EAST, chatOutputLabel);
			layout.putConstraint(SpringLayout.NORTH, gameOutputLabel, 0, SpringLayout.NORTH, chatOutputLabel);
			layout.putConstraint(SpringLayout.SOUTH, gameOutputLabel, 18, SpringLayout.NORTH, gameOutputLabel);
			layout.putConstraint(SpringLayout.EAST, gameOutputLabel, 333, SpringLayout.WEST, gameOutputLabel);
		connOutputLabel.setFont(new Font("Arial", Font.BOLD, 16));
		connOutputLabel.setText("Server Connection Comms:");
		frame.add(connOutputLabel);
			layout.putConstraint(SpringLayout.WEST, connOutputLabel, 5, SpringLayout.EAST, gameOutputLabel);
			layout.putConstraint(SpringLayout.NORTH, connOutputLabel, 0, SpringLayout.NORTH, chatOutputLabel);
			layout.putConstraint(SpringLayout.SOUTH, connOutputLabel, 18, SpringLayout.NORTH, connOutputLabel);
			layout.putConstraint(SpringLayout.EAST, connOutputLabel, 333, SpringLayout.WEST, connOutputLabel);
	}
	
	private void setupChatOutput(){
		
		chatOutput.setEditable(false);
		tempBorder = BorderFactory.createLoweredBevelBorder();
			chatOutput.setBorder(tempBorder);
		chatOutput.setAutoscrolls(true);
		chatScroll = new JScrollPane(chatOutput, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		frame.add(chatScroll);
			layout.putConstraint(SpringLayout.NORTH, chatScroll, 5, SpringLayout.SOUTH, chatOutputLabel);
			layout.putConstraint(SpringLayout.WEST, chatScroll, 5, SpringLayout.WEST, frame);
			layout.putConstraint(SpringLayout.EAST, chatScroll, 338, SpringLayout.WEST, frame);
			layout.putConstraint(SpringLayout.SOUTH, chatScroll, 325, SpringLayout.NORTH, chatScroll);
	}
	
	private void setupGameOutput(){
		
		gameOutput.setEditable(false);
		tempBorder = BorderFactory.createLoweredBevelBorder();
			gameOutput.setBorder(tempBorder);
		gameOutput.setAutoscrolls(true);
		gameScroll = new JScrollPane(gameOutput, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		frame.add(gameScroll);
			layout.putConstraint(SpringLayout.NORTH, gameScroll, 0, SpringLayout.NORTH, chatScroll);
			layout.putConstraint(SpringLayout.WEST, gameScroll, 5, SpringLayout.EAST, chatScroll);
			layout.putConstraint(SpringLayout.EAST, gameScroll, 333, SpringLayout.WEST, gameScroll);
			layout.putConstraint(SpringLayout.SOUTH, gameScroll, 0, SpringLayout.SOUTH, chatScroll);
	}
	
	private void setupConnOutput(){
		
		connOutput.setEditable(false);
		tempBorder = BorderFactory.createLoweredBevelBorder();
			connOutput.setBorder(tempBorder);
		connOutput.setAutoscrolls(true);
		connScroll = new JScrollPane(connOutput, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		frame.add(connScroll);
			layout.putConstraint(SpringLayout.NORTH, connScroll, 0, SpringLayout.NORTH, chatScroll);
			layout.putConstraint(SpringLayout.WEST, connScroll, 5, SpringLayout.EAST, gameScroll);
			layout.putConstraint(SpringLayout.EAST, connScroll, 333, SpringLayout.WEST, connScroll);
			layout.putConstraint(SpringLayout.SOUTH, connScroll, 0, SpringLayout.SOUTH, chatScroll);
	}
}
